package com.upb.webserver.company;

import com.upb.webserver.common.exception.WebServerException;
import com.upb.webserver.common.returncode.ConstantCode;
import com.upb.webserver.common.tools.JsonUtils;
import com.upb.webserver.common.tools.MybatisExampleTools;
import com.upb.webserver.dao.entity.TbCompany;
import com.upb.webserver.dao.entity.TbCompanyDeveloper;
import com.upb.webserver.dao.entity.TbCompanyDeveloperExample;
import com.upb.webserver.dao.mapper.TbCompanyDeveloperMapper;
import com.upb.webserver.dao.mapper.TbCompanyMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * institutional developer company table.
 */
@Slf4j
@Service
public class CompanyDeveloperService {

    @Autowired
    private TbCompanyDeveloperMapper tbCompanyDeveloperMapper;
    @Autowired
    private TbCompanyMapper tbCompanyMapper;

    /**
     * @param companyId
     * @param developerId
     * @param createBy
     * @return
     */
    @Transactional
    public TbCompanyDeveloper newCompanyDeveloper(int companyId, int developerId,byte platform, int createBy) {
        TbCompanyDeveloper tbCompanyDeveloper = new TbCompanyDeveloper();
        tbCompanyDeveloper.setCompanyId(companyId);
        tbCompanyDeveloper.setDeveloperId(developerId);
        tbCompanyDeveloper.setCreateBy(createBy);
        tbCompanyDeveloper.setCreateFromPlatform(platform);
        tbCompanyDeveloper.setGmtCreate(new Date());
        tbCompanyDeveloper.setGmtModified(new Date());

        tbCompanyDeveloperMapper.insertSelective(tbCompanyDeveloper);
        log.info("exec method [newCompanyDeveloper] success, return:{}", JsonUtils.objToString(tbCompanyDeveloper));
        return tbCompanyDeveloper;
    }


    /**
     * @param developerId
     * @return
     */
    public Integer selectCompanyIdByDeveloperId(int developerId) {
        TbCompanyDeveloper tbCompanyDeveloper = tbCompanyDeveloperMapper.selectByDeveloper(developerId);
        if (tbCompanyDeveloper != null) {
            return tbCompanyDeveloper.getCompanyId();
        }
        log.info("unsuccessfully exec method [selectCompanyIdByDeveloperId], not found record by developerId:{}", developerId);
        throw new WebServerException(ConstantCode.INVALID_DEVELOPER_ID);
    }

    /**
     * @param developerId
     * @return
     */
    public String selectCompanyNameByDeveloperId(Integer developerId) {
        TbCompanyDeveloper tbCompanyDeveloper = tbCompanyDeveloperMapper.selectByDeveloper(developerId);
        if (Objects.isNull(tbCompanyDeveloper)) {
            log.info("unsuccessfully exec method [selectCompanyIdByDeveloperId], not found record by developerId:{}", developerId);
            throw new WebServerException(ConstantCode.INVALID_DEVELOPER_ID);
        }

        TbCompany tbCompany = tbCompanyMapper.selectByPrimaryKey(tbCompanyDeveloper.getCompanyId());
        return tbCompany.getCompanyName();

    }


    /**
     * @param companyIdList
     * @return
     */
    public List<Integer> ListDeveloperIdByCompanyIdList(List<Integer> companyIdList) {
        List<TbCompanyDeveloper> companyDeveloperList = tbCompanyDeveloperMapper.selectByCompanyIdList(companyIdList);
        if (Objects.isNull(companyDeveloperList) || companyDeveloperList.size() == 0) {
            log.info("unsuccessfully exec method [ListCompanyIdByDeveloperIdList], not found record by companyIdList:{}", JsonUtils.objToString(companyIdList));
            throw new WebServerException(ConstantCode.INVALID_COMPANY_ID);
        }
        return companyDeveloperList.stream().map(cd -> cd.getDeveloperId()).collect(Collectors.toList());
    }
}
